package com.fdq.dianping.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdq.dianping.entity.Blog;
import com.fdq.dianping.entity.Result;
import com.fdq.dianping.entity.dto.UserDto;
import com.fdq.dianping.service.IBlogService;
import com.fdq.dianping.util.AliOss;
import com.fdq.dianping.util.ThreadLocalUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static com.fdq.dianping.util.Constants.Page_Size;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fdq
 * @since 2024-12-29
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private IBlogService blogService;


    /*
    * 查看热门点评
    * */
    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current){
        return blogService.queryHotBlog(current);
    }



    /*
    * 根据id查询点评
    * */
    @GetMapping("/{id}")
    public Result queryBlogById(@PathVariable("id") Long id){
        return blogService.queryBlogById(id);
    }


    /*
    * 查看点赞的用户
    * */
    @GetMapping("/likes/{id}")
    public Result queryBlogLikes(@PathVariable("id") Long id){
        return blogService.queryBlogLikes(id);
    }


    /*
    * 当前用户给点评点赞
    * */
    @PostMapping("/like/{id}")
    public Result likeBlog(@PathVariable("id") Long id){
        return blogService.likeBlog(id);
    }


    /**
     * 保存点评并使用推模式将点评发送给用户（在redis中保存，按时间排序发送给用户）
     */
    @PostMapping
    public Result saveBlog(@RequestBody Blog blog){
        return blogService.saveBlog(blog);
    }

    @PostMapping("/picUpload")
    public Result picUpload(MultipartFile pic) throws IOException {
        // 生成一个随机的UUID
        UUID uuid = UUID.randomUUID();
        // 将UUID转换为字符串
        String uuidString = uuid.toString();
        // 去掉连字符，生成一个纯随机字符串
        String randomString = uuidString.replace("-", "");
        String fileName = randomString+".jpg";
        String url = AliOss.upload(fileName, pic);
        System.out.println("url:"+url);
        return Result.success(url);
    }


    /**
     *
     * 查询某用户发的点评
     */
    @GetMapping("/of/user")
    public Result queryByUserId(@RequestParam(value = "current",defaultValue = "1") Integer current,
                                @RequestParam("id") Long id){
        Page<Blog> page = blogService.query().eq("user_id", id).page(new Page<Blog>(current, Page_Size));
        List<Blog> blogs = page.getRecords();
        return Result.success(blogs);
    }

    /**
     * 查询关注的人发布的点评
     */
    @GetMapping("/of/follow")
    public Result queryBlogOfFollow(@RequestParam(value = "lastId",required = false) Long max,
                                    @RequestParam(value = "offset",defaultValue = "0") Integer offset){
        if(max == 0){
            max = System.currentTimeMillis();
        }
        return blogService.queryBlogOfFollow(max, offset);
    }


    /**
     * 根据id删除点评
     */
    @DeleteMapping
    public Result deleteBlogById(@RequestParam Long id){
        boolean remove = blogService.removeById(id);
        if(remove){
            return Result.success();
        }else {
            return Result.error("删除点评失败");
        }
    }


}
